<template>
  <div class="scan">
    <img :src="url" alt="">
  </div>
</template>

<script>
import { bindQrcode, state } from '@/api/system/personalCenter'
export default {
  props: {
    layerid: {}
  },
  data() {
    return {
      url: '',
      timer: undefined,
      state: false
    }
  },
  mounted() {
    this.bindQrcode()
    this.timer = setInterval(() => {
      if (this.state) {
        this.$message.success('绑定成功')
        clearInterval(this.timer)
        this.$layer.close(this.layerid)
        setTimeout(() => {
          this.$router.go(0)
        }, 1500)
      } else {
        this.polling()
      }
    }, 1000)
  },
  beforeDestroy() {
    clearInterval(this.timer)
  },
  methods: {
    // 绑定微信
    bindQrcode() {
      bindQrcode().then(res => {
        this.url = res.data
      })
    },
    // 轮询
    polling() {
      state().then(res => {
        this.state = res.data
      })
    }

  }
}
</script>

<style lang="less" scoped>
.scan{
    display: flex;
    justify-content:center;
    align-items: center;
    width: 100%;
    img{
        height: 200px;
        width: 200px;
    }
}
</style>

